package com.htthorizon.model.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.htthorizon.common.security.utils.SecurityUtils;
import com.htthorizon.model.domain.ModelCrossAudit;
import com.htthorizon.model.mapper.ModelCrossAuditMapper;
import com.htthorizon.model.request.ModelCrossAuditAddReq;
import com.htthorizon.model.request.ModelCrossAuditSearchReq;
import com.htthorizon.model.response.ModelCrossAuditVo;
import com.htthorizon.model.response.ModelMessageVo;
import com.htthorizon.system.api.RemoteUserService;
import com.htthorizon.system.api.domain.SysUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Brilliant ,
 */
@Service
public class ModelCrossAuditServiceImpl implements IModelCrossAuditService{

    @Autowired
    private ModelCrossAuditMapper modelCrossAuditMapper;

    @Autowired
    private RemoteUserService remoteUserService;

    @Override
    public List<ModelCrossAuditVo> getList(ModelCrossAuditSearchReq search) {
        int pageNum=Integer.parseInt((String)search.getParams().get("pageNum"));
        int pageSize=Integer.parseInt((String)search.getParams().get("pageSize"));
      List<ModelCrossAudit> list = modelCrossAuditMapper.selectBySearch(search,(pageNum-1)*pageSize,pageSize);
        return list.stream().map(this::map).collect(Collectors.toList());
    }

    @Override
    public boolean add(ModelCrossAuditAddReq modelCrossAuditAddReq) {
//        SysUser userById = remoteUserService.getUserById(Long.valueOf(modelCrossAuditAddReq.getModelAuthor()));
//        modelCrossAuditAddReq.setModelAuthor(userById.getNickName());
        return modelCrossAuditMapper.insertSelective(modelCrossAuditAddReq) > 0;
    }

    @Override
    public boolean edit(ModelCrossAuditAddReq modelCrossAuditAddReq) {
        return modelCrossAuditMapper.updateByPrimaryKeySelective(modelCrossAuditAddReq) > 0;
    }

    @Override
    public boolean del(Long id) {
        return modelCrossAuditMapper.deleteByPrimaryKey(id) > 0;
    }

    private ModelCrossAuditVo map(ModelCrossAudit modelCrossAudit){
        ModelCrossAuditVo modelCrossAuditVo = new ModelCrossAuditVo();
        BeanUtils.copyProperties(modelCrossAudit,modelCrossAuditVo);
        DateTime startTime = DateUtil.date(modelCrossAudit.getEmpowerStartTime());
        DateTime endTime = DateUtil.date(modelCrossAudit.getEmpowerEndTime());
        DateTime now = DateTime.now();

        long between = DateUtil.between(startTime, endTime, DateUnit.DAY);
        modelCrossAuditVo.setDueDate(between);
        if (endTime.isBefore(now)){
            modelCrossAuditVo.setStatus("已到期");
            modelCrossAuditVo.setDueDate(0L);
        }else {
            modelCrossAuditVo.setStatus("未到期");
        }
        Long modelEmpowerId = modelCrossAudit.getModelEmpowerId();
        SysUser userById = remoteUserService.getUserById(modelEmpowerId);
            modelCrossAuditVo.setEmpowerNickName(userById.getNickName());
            modelCrossAuditVo.setEmpowerUserName(userById.getUserName());
        return modelCrossAuditVo;
    }

    @Override
    public List<String> getModelCrossAuditStringList() {
        DateTime date = DateUtil.date();
        Long userId = SecurityUtils.getUserId();
        List<ModelCrossAudit> list =  modelCrossAuditMapper.getModelCrossAudit(date,userId);
        List<String> strings=new ArrayList<>();
        for (ModelCrossAudit modelCrossAudit:list){
            String modelEmpower = modelCrossAudit.getModelEmpower();
            JSONArray jsonArray = JSON.parseArray(modelEmpower);
            for (int i = 0; i < jsonArray.size(); i++) {
                strings.add((String) jsonArray.get(i));
            }
        }
        Set<String> objects = new HashSet<>(strings);
        strings.clear();
        strings.addAll(objects);
        return strings;
    }

    @Override
    public int getCount(ModelCrossAuditSearchReq modelCrossAuditSearchReq) {
        return modelCrossAuditMapper.selectCount(modelCrossAuditSearchReq);
    }
}
